package com.shievy.anweb.webview.web.dispatch

import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import com.shievy.anweb.webview.IJSToNativeAidlInterface
import com.shievy.anweb.webview.INativeToJSAidlInterface
import com.shievy.anweb.webview.main.WebTelecomService
import com.shievy.anweb.webview.web.view.BaseWebView
import java.lang.ref.WeakReference

/**
 * WebView js数据事件分发与服务绑定
 * */
class WebDispatcher private constructor() : ServiceConnection {
    /**
     * 跨进程通讯Binder
     * */
    private var jsToNative: IJSToNativeAidlInterface? = null

    /**
     * 本地进程跨进通知JS
     * */
    private var nativeToJs: INativeToJSAidlInterface? = null

    private var weekContext: WeakReference<Context>? = null

    companion object {
        private var instance: WebDispatcher? = null
        fun getWebTelecomService(): WebDispatcher? {
            if (instance == null) {
                synchronized(WebDispatcher::class) {
                    instance = WebDispatcher()
                    return instance
                }
            } else {
                return instance
            }
        }
    }


    /**
     * 创建跨进程通讯链接
     * */
    fun createConnection(context: Context) {
        if (weekContext == null) {
            weekContext = WeakReference(context)
        }
        weekContext?.get()?.let {
            val intent = Intent(context, WebTelecomService::class.java)
            context.bindService(intent, this, Context.BIND_AUTO_CREATE)
        }
    }

    override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
        jsToNative = IJSToNativeAidlInterface.Stub.asInterface(service)
    }

    override fun onServiceDisconnected(name: ComponentName?) {
        jsToNative = null
//        weekContext?.get()?.let {
//            createConnection(it)
//        }

    }

    override fun onBindingDied(name: ComponentName?) {
        jsToNative = null
//        weekContext?.get()?.let {
//            createConnection(it)
//        }
    }

    fun unBind() {
        weekContext?.get()?.let {
            it.unbindService(this)
        }
        jsToNative = null
        instance = null
        weekContext = null
    }

    /**
     *
     * @param telecomID 通讯ID
     * @param params 通讯参数
     * @param  jsMethodID 需要回调的js方法ID
     * */
    fun callNativeMain(telecomID: String?, params: String?, jsMethodID: String?, web: BaseWebView) {
        if (jsMethodID.isNullOrEmpty()||jsMethodID.isNullOrBlank())
            jsToNative?.callNativeMain(telecomID, params, jsMethodID)
        else {
            if (nativeToJs == null) {
                nativeToJs = object : INativeToJSAidlInterface.Stub() {
                    override fun callWebJs(
                        telecomID: String?,
                        params: String?,
                        jsMethodID: String?
                    ) {
                        web.takeJsAction(jsMethodID, params)
                    }

                }
            }
            jsToNative?.callNativeBackToWebJs(telecomID, params, jsMethodID, nativeToJs)
        }

    }


}